package com.c.ctgproject.wiget.dialog

import android.app.Dialog
import android.content.Context
import android.view.Gravity
import android.view.WindowManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.bigkoo.pickerview.listener.OnOptionsSelectChangeListener
import com.c.ctgproject.R
import com.c.ctgproject.SimpleListAdapter

class MyBottomDialog : Dialog {
    //社会车 试验车 外包车 五类车 农机具
    // Dialog 里的可选项，可以是任何类型的数据
    //private val options = mutableListOf("社会车", "试验车", "外包车","五类车","农机具")

    private var optionListView:RecyclerView?=null
    private fun initView(){
        setContentView(R.layout.dialog_bottom_options)
        optionListView = findViewById<RecyclerView>(R.id.option_list_view)

    }
    constructor(context: Context, themeStyle: Int) : super(context, themeStyle) {

        initView()

    }
    class Builder(val context: Context) {
        // 选择回调函数
        private var onOptionSelected: ((Int) -> Unit)? = null
        private var options= emptyList<String>()
        // 设置选择回调函数
        fun setOnOptionSelectedListener(listener: ((Int) -> Unit)?):Builder {
            onOptionSelected = listener
            return this
        }
        fun setOptions(list:List<String>):Builder{
            options=list
            return this
        }

        fun create(): MyBottomDialog {
            val dialog = MyBottomDialog(context, R.style.CommonDialog)
            dialog.window?.setGravity(Gravity.BOTTOM)
            //设置不遮挡后面
            //dialog.window?.setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL, WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
            //获得window窗口的属性
            val lp = dialog.window?.attributes
            //设置窗口宽度为充满全屏
            //dialog.dialog_phone?.text = phonetext
            lp?.width = WindowManager.LayoutParams.MATCH_PARENT
            lp?.height = WindowManager.LayoutParams.WRAP_CONTENT

            dialog?.optionListView?.layoutManager= LinearLayoutManager(context)

            dialog?.optionListView?.adapter = SimpleListAdapter(context, options,object :SimpleListAdapter.CallBack{
                override fun onItemClick(position: Int) {
                    onOptionSelected?.invoke(position)
                    dialog?.dismiss()
                }

            })
            return dialog
        }
    }

//    override fun onCreate(savedInstanceState: Bundle?) {
//        super.onCreate(savedInstanceState)
//        val window = this.window
//        window?.setGravity(Gravity.BOTTOM) // 设置 Dialog 显示在底部
//        window?.setType(R.style.common_dialog)
//        //window?.decorView?.background = null
//
//        val params = window?.attributes
//
//
//        window?.attributes = params
//        params?.width = WindowManager.LayoutParams.MATCH_PARENT // Dialog 宽度全屏
//        params?.height = WindowManager.LayoutParams.WRAP_CONTENT // Dialog 高度自适应
//
//
//        // 设置 Dialog 的宽度为屏幕宽度
//        window?.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
//
//        // 将选项添加到列表中
//        val optionListView = findViewById<RecyclerView>(R.id.option_list_view)
//        optionListView.layoutManager= LinearLayoutManager(context)
//
//        optionListView.adapter = SimpleListAdapter(context, options,object :SimpleListAdapter.CallBack{
//            override fun onItemClick(position: Int) {
//                onOptionSelected?.invoke(position)
//                dismiss()
//            }
//
//        })
//        val adapter = ArrayAdapter(context, android.R.layout.simple_list_item_1, options)
//
//        // 添加取消按钮点击事件
////        val cancelButton = findViewById<Button>(R.id.cancel_button)
////        cancelButton.setOnClickListener { dismiss() }
//    }

}
